﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;


namespace Monitor.Core.Sensor
{
    public class StormSensor : ICompositeSensor
    {
        private int _id;
        public StormSensor(int id)
        {
            _id = id;
            _supervisors = new List<StormSupervisorSensor>();
        }

        private StormNimbusSensor _nimbus;
        private List<StormSupervisorSensor> _supervisors;
        public void AddComponent(ISensor sensor)
        {
            var nimbus = sensor as StormNimbusSensor;
            if (nimbus != null)
                _nimbus = nimbus;

            var supervisor = sensor as StormSupervisorSensor;
            if (supervisor != null)
            {
                _supervisors.Add(supervisor);
            }
        }

        public int Id
        {
            get { return _id; }
        }

        private string _description;
        public string Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }

        public ServiceStatus Ping()
        {
            _lastRefreshTime = DateTime.Now;
            _nimbus.Ping();
            var summary = _nimbus.Summary;


            if (summary.Nimbus_uptime_secs > 0)
            {
                if (summary.Supervisors.Count == _supervisors.Count)
                {
                    foreach (var sup in summary.Supervisors)
                    {
                        if (sup.Uptime_secs <= 0)
                        {
                            _lastError = this.Description + ": " + string.Format("At least one supervisor unstable.");
                            _status = ServiceStatus.Unstable;
                            return _status;
                        }
                    }
                    _status = ServiceStatus.Alive;
                    _lastError = string.Empty;
                    return _status;
                }
                else
                {
                    _lastError = this.Description + ": " + string.Format("Expect {0} supervisors, but only {1} remain.", _supervisors.Count, summary.Supervisors.Count);
                    _status = ServiceStatus.Unstable;
                    return _status;
                }
            }
            else
            {
                _lastError = _nimbus.LastError;
                _status = ServiceStatus.Dead;
                return _status;
            }
        }

        private DateTime _lastRefreshTime;
        public DateTime LastRefreshTime
        {
            get { return _lastRefreshTime; }
        }

        public int Interval
        {
            get;
            set;
        }

        private ServiceStatus _status;
        public ServiceStatus Status
        {
            get { return _status; }
        }

        private string _lastError = string.Empty;
        public string LastError
        {
            get
            {
                return _lastError;
            }
        }
    }
}
